use crate::Stack;
use std::str::Chars;

//同时检测多种开始和结束符号是否匹配
const OPENS: &str = "([{";
const CLOSES: &str = ")]}";
fn paren_exists(open: char, close: char) -> bool {
    OPENS.find(open) == CLOSES.find(close)
}

fn contains(c: char, c_list: Chars) -> bool {
    for ch in c_list {
        if c == ch {
            return true;
        }
    }
    false
}

pub fn paren_match_muls(parens: &str) -> bool {
    let mut char_list = Vec::new();
    for paren in parens.chars() {
        char_list.push(paren);
    }

    let mut index = 0;
    let mut balance = true;
    let mut stack = Stack::new();

    while index < char_list.len() && balance {
        let c = char_list[index];
        if contains(c, OPENS.chars()) {
            stack.push(c);
        } else {
            if stack.is_empty() {
                balance = false
            } else {
                let top = stack.pop().unwrap();
                if !paren_exists(top, c) {
                    balance = false
                }
            }
        }
        index += 1;
    }
    balance && stack.is_empty()
}
pub fn paren_match_muls_ignore(parens: &str) -> bool {
    let mut char_list = Vec::new();
    for paren in parens.chars() {
        char_list.push(paren);
    }

    let mut index = 0;
    let mut balance = true;
    let mut stack = Stack::new();

    while index < char_list.len() && balance {
        let c = char_list[index];
        if contains(c, OPENS.chars()) {
            stack.push(c);
        } else if contains(c, CLOSES.chars()) {
            if stack.is_empty() {
                balance = false
            } else {
                let top = stack.pop().unwrap();
                if !paren_exists(top, c) {
                    balance = false
                }
            }
        }
        index += 1;
    }
    balance && stack.is_empty()
}
